// components/footer/footer.js
var app = getApp()
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    isHistory: {
      type: String,
      value: -1
    },
    storageNum: {
      type: Number,
      default: 0,
      observer: function (newVal, oldVal) {
        this.setData({
          questionNumber: newVal
        })
      }
    },
    asList: {
      type: Array,
      value: [],
      observer: function (newVal, oldVal) {
        if (newVal.length > 0) {
          newVal.forEach((v, k) => {
            v = Object.assign(v, { isAnswered: v.isAnswered || '' , selectedId: v.selectedId || '' })
            newVal[k] = v
            let list = v.list
            list.forEach((value, key) => {
              let obj = Object.assign(value, { class: value.class || '' })
              list[key] = obj
            })
          })
          this.setData({
            answerList: newVal
          })
        }
      }
    },
    unitId: {
      type: String,
      value: ''
    },
    cheight: {
      type: Number,
      value: '',
      observer: function (newVal, oldVal) {
      }
    }
  },
  /**
   * 组件的初始数据
   */
  data: {
    answerList: [],
    questionNumber: 0,
    selectedAnswer: {},
    answerNumber: 0, //已经回答的题目数
    successVideo: null,
    failVideo: null
  },

  /**
   * 组件的方法列表
   */
  methods: {
    bindchange(event) {
      let swiper = event.detail
      let cur = swiper.current 
      cur = cur - 1 > 0 ? cur - 1 : 0
      if (!this.data.answerList[cur].isAnswered) {
        // 如果是未答题
        this.setData({
          questionNumber: this.data.questionNumber
        })
        if (this.properties.isHistory === -1) {
          // 如果是答题列表而不是历史题目列表
          wx.showToast({
              icon: 'none',
               title: '答完这题才能进入下一题哦',
          })
        }
      }
      else {
        this.setData({
          questionNumber: swiper.current
        })
      }
    },
    selectAnswer(e) {
      if (this.properties.isHistory === -1) {
        let dataset = e.currentTarget.dataset
        let answer = dataset.answer
        let isAnswered = dataset.isanswered
        let _self = this
        if (!isAnswered) {
          let cangoNext = false
          let answerList = this.data.answerList
          let question = answerList[this.data.questionNumber]
          question.isAnswered = true
          question.selectedId = answer.id
          /* this.setData({
            selectedAnswer: answer
          }) */
          question.list.forEach((v, k) => {
            if (v.is_true) {
              v.class = "right"
            }
            if (!v.is_true && answer.id === v.id) {
              v.class = "wrong"
              this.playvideo(-1)
            }
            if (!v.is_true && answer.id !== v.id) {
              v.class = "grey"
            }
            if (v.is_true && answer.id === v.id) {
              cangoNext = true
              this.playvideo(1)
            }
          })
          answerList[this.data.questionNumber] = question
          this.setData({
            answerList: answerList,
            answerNumber: this.data.answerNumber + 1
          })
          // 答题完自动跳转到下一页
          let number = this.data.questionNumber + 1
          if (number < this.data.answerList.length) {
            // 设定时间的目的是为了让声音可以播放完全再进入下一段
            setTimeout(() => {
              this.setData({
                questionNumber: number
              })
            },100)
          }
          if (number === this.data.answerList.length) {
            wx.showLoading({
              title: '恭喜答题完成',
            })
            app.removeQAListStorage()
            if (this.properties.unitId) {
              setTimeout(() => {
                wx.hideLoading()
                let answers = []
                this.data.answerList.map((v, k) => {
                  answers.push(v.selectedId)
                })
                app.requestModule('/app/plan/addUserAnswer', { unitId: this.properties.unitId, answers: answers }, this.addanswerCallback, () => { }, 'POST')
              }, 500)
            }
          }
        }
        else {
          wx.showModal({
            title: '提示',
            content: '您已经答过该题，不能重复答题哦',
          })
        }
      }
    },
    addanswerCallback(res) {
      app.globalData.isFinishedcourse = res.data
      if (app.globalData.isFinishedcourse === 1 ) {
        wx.showModal({
          title: '提示',
          content: '您设定的计划已经完成，让我们去添加计划吧',
          success: (res) => {
            app.formDirectUrl('/pages/studyPlanAdd/studyPlanAdd')
          }
        })
      }
      else {
        app.formDirectUrl('/pages/done/done')
      }
    },
    playvideo(flag) {
      if (flag === 1) {
        this.data.successVideo.stop()
        this.data.failVideo.stop()
        this.data.successVideo.play()
        this.data.successVideo.onEnded(() => {
          this.data.successVideo.stop()
        })
      }
      else if(flag === -1) {
        this.data.successVideo.stop()
        this.data.failVideo.stop()
        this.data.failVideo.play()
        this.data.failVideo.onEnded(() => {
          this.data.failVideo.stop()
        })
      }
    }
  },
  created () {
    const innerAudioContext = wx.createInnerAudioContext()
    innerAudioContext.src = 'http://p9h3lexjs.bkt.clouddn.com/shake_match.mp3'
    
    const innerAudioContext2 = wx.createInnerAudioContext()
    innerAudioContext2.src = 'http://p9h3lexjs.bkt.clouddn.com/shake_nomatch.mp3'
    this.setData({
      successVideo: innerAudioContext,
      failVideo: innerAudioContext2
    })
  },
  attached () {
    
  },
  moved () {
  },
  detached () {
    // 将用户答题的题目数记录到缓存
    let count = 0
    this.data.answerList.filter((v, k) => {
      if (v.isAnswered) count++
    })
    console.log('count: ', count)
    if (count < this.data.answerList.length) {
      wx.setStorageSync('QAlistNum', this.data.questionNumber)
      wx.setStorageSync('QAlist', this.data.answerList)
      wx.setStorageSync('QAunitId', this.properties.unitId)
    }
  }
})
